<?php

Class Application_Model_Mapper_Users {

	protected $_dbTable = null;

	/**
	 * Конструктор
	 * @param Application_Model_DbTable_Users $dbTable
	 * @param Zend_Db_Adapter_Abstract $dbAdapter
	 * @return Application_Model_Mapper_Users
	 */
	public function  __construct($dbTable = null) {
		if ($dbTable) $this->setDbTable($dbTable);
		return $this;
	}

	/**
	 *
	 * @param Application_Model_Users $user
	 * @return Application_Model_Mapper_Users
	 */
	public function insert(Application_Model_Users $user) {
		$data = $user->toArray();
		$this->getDbTable()->insert($data);
		return $this;
	}

	/**
	 *
	 * @param Application_Model_Users $user
	 * @return Application_Model_Mapper_Users
	 */
	public function update(Application_Model_Users $user) {
		$data = $user->toArray();
		$this->getDbTable()->update($data);
		return $this;
	}

	/**
	 *
	 * @param Application_Model_Users $user
	 * @return Application_Model_Mapper_Users
	 */
	public function delete(Application_Model_Users $user) {
		$id = $user->getId();
		$this->getDbTable()->delete($id);
		return $this;
	}

	/**
	 *
	 * @param Application_Model_Users $user
	 * @return Application_Model_Mapper_Users
	 */
	public function save(Application_Model_Users $user) {
		if ($user->getId()) {
			$this->update($user);
		} else {
			$this->insert($user);
		}
		return $this;
	}

	/**
	 * Добавляет нового пользователя
	 * @param string $login
	 * @param string $password
	 * @param string $fio
	 * @param string $date
	 * @return bool
	 */
	public function add($login, $password, $fio, $date) {
		$data = array(
			'login' => $login,
			'password' => $password,
			'fio' => $fio,
			'date' => $date
		);

		$user = new Application_Model_Users($data);
		$this->save($user);

		return true;
	}

	/**
	 * Ищет и возвращает пользователя по его $id
	 * @param int $id
	 * @return bool||Application_Model_Users
	 */
	public function findById($id) {
		$row = $this->getDbTable()->find($id);
		if (0 == count($row)) return false;
		return new Application_Model_Users($row);
	}

	/**
	 * Ищет и возвращает пользователя по его $login
	 * @param string $login
	 * @return bool||Application_Model_Users
	 */
	public function findByLogin($login) {
		$select = $this->getDbTable()->select()
			->from(array('u'=>'users'), array('id', 'login', 'fio', 'date'))
			->where('u.login = ?', $login);
		$stmt = $select->query();
		$row = $stmt->fetch();
		if (!count($row)) return false;
		return new Application_Model_Users($row);
	}

	/**
	 *
	 * @param <type> $id
	 * @return <type>
	 */
	public function getRoleById($id) {

		$sql = 'SELECT rol.name
			FROM users_to_roles AS ur
			LEFT JOIN acl_roles AS rol ON ur.role = rol.id
			WHERE ur.user = ' . $id;
		$stmt = $this->getDbTable()->getAdapter()->query($sql);
		$result = $stmt->fetchColumn();
		return $result;
	}

	/**
	 *
	 * @param <type> $login
	 */
	public function getRoleByLogin($login) {

	}

	/**
	 *
	 * @param string||Application_Model_DbTable_Users $dbTable
	 * @return Application_Model_Mapper_Users
	 */
	public function setDbTable($dbTable) {
		if (is_string($dbTable)) {
			$dbTable = new $dbTable();
		}
		if (!$dbTable instanceof Zend_Db_Table_Abstract) {
			throw new Exception('Invalid table data gateway provided');
		}
		$this->_dbTable = $dbTable;
		return $this;
	}

	public function getDbTable() {
		if (null === $this->_dbTable) {
			$this->setDbTable('Application_Model_DbTable_Users');
		}
		return $this->_dbTable;
	}

	public function getUsersList($rate, $perpage) {
		//$userDbTable = new Application_Model_DbTable_Users();
		/*
		$stmt = $this->_dbTable->getAdapter()->select()
			->from(array('g' => 'Users'), array('id', 'path', 'text', 'date'))
			->joinLeft(array('u' => 'users'), 'u.id = g.user', array('login'))
			->order('g.path')
			->limit($perpage, $rate * $perpage - $perpage);
		*/
		$sql = 'SELECT g.id, g.path, g.text, g.date, u.login, u.fio
			FROM Users AS g
			INNER JOIN users AS u ON u.id = g.user
			ORDER BY g.path ASC LIMIT ' . ($rate * $perpage - $perpage) . ', ' . $perpage;

		$stmt = $this->getDbTable()->getAdapter()->fetchAll($sql);
		return $stmt;
	}
}